﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AG.Editor.Core.Data;
using System.IO;

namespace AG.Editor.Core.Stores
{
    public class AGMapStore
    {
        /// <summary>
        /// 保存地图信息
        /// </summary>
        /// <param name="project"></param>
        /// <param name="map"></param>
        public void SaveMap(AGEProject project, AGMap map)
        {
            string folderMaps = project.GetFolderMaps();
            if (!Directory.Exists(folderMaps))
            {
                Directory.CreateDirectory(folderMaps);
            }

            string mapFilePath = Path.Combine(project.GetFolderMaps(), string.Format("{0:d4}.map", map.Id));

            FileStream stream = null;
            if (File.Exists(mapFilePath))
            {
                stream = File.OpenWrite(mapFilePath);
            }
            else
            {
                stream = File.Create(mapFilePath);
            }

            StreamWriter writer = new StreamWriter(stream);

            #region #tiles
            writer.WriteLine("#tiles");
            writer.WriteLine("--id");
            writer.WriteLine(map.Id);
            writer.WriteLine("--version");
            writer.WriteLine("2");
            writer.WriteLine("--name");
            writer.WriteLine(map.Name);
            writer.WriteLine("--row");
            writer.WriteLine(map.Row);
            writer.WriteLine("--col");
            writer.WriteLine(map.Col);
            writer.WriteLine("--terrain");
            for (ushort rowIndex = 0; rowIndex < map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < map.Col; colIndex++)
                {
                    AGMapTile tile = map.GetTile(rowIndex, colIndex);
                    writer.Write("{0},", tile.TerrainId);
                }
                writer.WriteLine();
            }

            writer.WriteLine("--tile id");
            for (ushort rowIndex = 0; rowIndex < map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < map.Col; colIndex++)
                {
                    AGMapTile tile = map.GetTile(rowIndex, colIndex);
                    writer.Write("{0},", tile.TileId);
                }
                writer.WriteLine();
            }

            writer.WriteLine("--tile index");
            for (ushort rowIndex = 0; rowIndex < map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < map.Col; colIndex++)
                {
                    AGMapTile tile = map.GetTile(rowIndex, colIndex);
                    writer.Write("{0},", tile.TileIndex);
                }
                writer.WriteLine();
            }

            writer.WriteLine("--tile block");
            for (ushort rowIndex = 0; rowIndex < map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < map.Col; colIndex++)
                {
                    AGMapTile tile = map.GetTile(rowIndex, colIndex);
                    if (tile.IsBlock)
                    {
                        writer.Write("1,");
                    }
                    else
                    {
                        writer.Write("0,");
                    }
                }
                writer.WriteLine();
            }
            #endregion

            #region #objects
            writer.WriteLine("#objects");
            #endregion

            #region #switchpoints
            writer.WriteLine("#switchpoints");
            #endregion

            writer.Close();
            stream.Close();
        }

        /// <summary>
        /// 加载地图
        /// </summary>
        /// <param name="project"></param>
        /// <param name="mapRef"></param>
        /// <returns></returns>
        public AGMap LoadMap(AGEProject project, AGMapRef mapRef)
        {
            string mapFilePath = Path.Combine(project.GetFolderMaps(), string.Format("{0:d4}.map", mapRef.MapId));
            if (!File.Exists(mapFilePath))
            {
                // 文件不存在
                return null;
            }

            AGMap map = new AGMap();

            using (FileStream stream = File.OpenRead(mapFilePath))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    int segment = 0;
                    int step = 0;
                    string lineText = reader.ReadLine();
                    while (lineText != null)
                    {
                        if (lineText == "#tiles")
                        {
                            segment = 0;
                            step = 0;
                        }
                        else if (lineText == "#objects")
                        {
                            segment = 1;
                            step = 0;
                        }
                        else if (lineText == "#switchpoints")
                        {
                            segment = 2;
                            step = 0;
                        }
                        else if(!lineText.StartsWith("--"))
                        {
                            if (segment == 0)
                            {
                                if (step == 0)
                                {
                                    // id
                                    map.Id = Convert.ToUInt16(lineText);
                                }
                                else if (step == 1)
                                {
                                    // version
                                }
                                else if (step == 2)
                                {
                                    // name
                                    map.Name = lineText;
                                }
                                else if (step == 3)
                                {
                                    // row
                                    map.Row = Convert.ToUInt16(lineText);
                                }
                                else if (step == 4)
                                {
                                    // col;
                                    map.Col = Convert.ToUInt16(lineText);
                                    map.Tiles = new AGMapTile[map.Row * map.Col];
                                }
                                else if (step > 4 &&step < 5 + map.Row)
                                {
                                    // terrain
                                    int row = step - 5;
                                    string[] arr = lineText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    for (int col = 0; col < arr.Length; col++)
                                    {
                                        AGMapTile cell = new AGMapTile();
                                        cell.TerrainId = Convert.ToByte(arr[col], 16);
                                        map.Tiles[row * map.Col + col] = cell;
                                    }
                                }
                                else if (step > 4 + map.Row && step < 5 + 2 * map.Row)
                                {
                                    // tile id
                                    int row = step - (5 + map.Row);
                                    string[] arr = lineText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    for (int col = 0; col < arr.Length; col++)
                                    {
                                        AGMapTile cell = map.Tiles[row * map.Col + col];
                                        cell.TileId = Convert.ToByte(arr[col], 16);
                                    }
                                }
                                else if (step > 4 + 2 * map.Row && step < 5 + 3 * map.Row)
                                {
                                    // tile index
                                    int row = step - (5 + 2 * map.Row);
                                    string[] arr = lineText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    for (int col = 0; col < arr.Length; col++)
                                    {
                                        AGMapTile cell = map.Tiles[row * map.Col + col];
                                        cell.TileIndex = Convert.ToByte(arr[col], 16);
                                    }
                                }
                            }
                            else if (segment == 1)
                            {
                            }
                            else if (segment == 2)
                            {
                            }
                            step++;
                        }


                        lineText = reader.ReadLine();
                    }
                }
            }
            return map;
        }
    }
}
